package com.pinusmusic.service.impl;

import com.pinusmusic.dao.FavoriteDao;
import com.pinusmusic.dao.impl.FavoriteDaoImpl;
import com.pinusmusic.domain.*;
import com.pinusmusic.service.FavoriteService;

import java.util.List;

public class FavoriteServiceImpl implements FavoriteService {

    FavoriteDao dao = new FavoriteDaoImpl();

    @Override
    public ResultInfo favoriteAdd(Favorite favorite) {
        ResultInfo info = new ResultInfo();
        String ErrorMsg = "";
        boolean flag = false;
        if (dao.isUid(favorite.getUid().toString())) {
            if (dao.isMid(favorite.getMid().toString())) {
                if (dao.addFavorite(favorite)) {
                    ErrorMsg = "收藏成功";
                    flag = true;
                } else {
                    ErrorMsg = "收藏失败";
                }
            } else {
                ErrorMsg = "mid不存在";
            }
        } else {
            ErrorMsg = "uid不存在";
        }
        info.setFlag(flag);
        info.setErrorMsg(ErrorMsg);
        return info;
    }

    @Override
    public ResultInfo favoriteDelete(int fid) {
        ResultInfo info = new ResultInfo();
        String ErrorMsg = "";
        boolean flag = false;
        if (dao.isFid(Integer.toString(fid))) {
            if (dao.deleteFavorite(fid)) {
                ErrorMsg = "删除收藏成功";
                flag = true;
            } else {
                ErrorMsg = "删除收藏失败";
            }
        } else {
            ErrorMsg = "fid不存在";
        }
        info.setFlag(flag);
        info.setErrorMsg(ErrorMsg);
        return info;
    }

    @Override
    public List<MusicInfo> favoriteFindByUid(int uid) {
            return dao.findFavoriteMusicsByUid(uid);
    }

    @Override
    public void deleteFavByMid(int mid) {
        dao.deleteFavByMid(mid);
    }

}
